package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.Userinfo;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UserInfoFromSpring;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/travel")
public class TravelController {
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelCommentService travelCommentService;
    @RequestMapping("")
    public String list(@ModelAttribute("qo") TravelQuery qo,Model model) {
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/list";
    }

    @RequestMapping("/input")
    public String input(Model model) {
        List<Destination> dests = destinationService.getDestsByDeep(3);
        model.addAttribute("dests",dests);
        return "travel/input";
    }
    @Value("${file.path}")
    private String path;
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, path);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Travel travel,@UserInfoFromSpring Userinfo userinfo) {
        travel.setAuthor(userinfo);
        Long id = travelService.saveOrUpdate(travel);
        AjaxResult result = AjaxResult.SUCCESS.addData(id);
        return result;
    }

    @RequestMapping("/test")
    @ResponseBody
    public Userinfo test(Userinfo userinfo) {
        return userinfo;
    }

    @RequestMapping("/detail")
    public String detail(Long id, Model model, @UserInfoFromSpring Userinfo userinfo) {
        Travel travel = travelService.get(id);
        TravelContent content = travelService.getContent(id);
        travel.setTravelContent(content);
        List<Destination> toasts = destinationService.getToasts(travel.getDest().getId());
        List<StrategyDetail> sds=strategyDetailService.getViewNumTop3(travel.getDest().getId());
        List<Travel> ts = travelService.getViewNumTop3(travel.getDest().getId());

        //评论列表 list
        TravelCommentQuery qo=new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setCurrentPage(1);
        qo.setPageSize(Integer.MAX_VALUE);
        Page query = travelCommentService.query(qo);
        model.addAttribute("list",query.getContent());

        model.addAttribute("toasts",toasts);
        model.addAttribute("detail",travel);
        model.addAttribute("sds",sds);
        model.addAttribute("ts",ts);
        return "travel/detail";
    }

    @RequestMapping("/commentAdd")
    public String commentAdd(TravelComment comment,@UserInfoFromSpring Userinfo userinfo,Model model,Long floor) {
        comment.setUserId(userinfo.getId());
        comment.setUsername(userinfo.getNickname());
        comment.setCity(userinfo.getCity());
        comment.setLevel(userinfo.getLevel());
        comment.setHeadUrl(userinfo.getHeadImgUrl());
        comment.setCreateTime(new Date());
         comment = travelCommentService.saveOrUpdate(comment);

        model.addAttribute("floor",travelCommentService.getCountByTravelId(comment.getTravelId()));
        model.addAttribute("c",comment);
        return "travel/commentTpl";
    }
}
